<template>
    <div>
        <el-upload
            class="avatar-uploader"
            action="http://localhost:9000/image"
            method="post"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
        >
        <!-- action="http://localhost:9000/image"
            :headers=head
            method="post"
            :name=imageUrl -->
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
        <button @click="getView">get</button>
    </div>
</template>

<script setup>
import { image,get,login } from '@/api/image';
import { ref } from 'vue';
import { Plus } from '@element-plus/icons-vue'

const imageUrl = ref('')

let head=ref({'Authorization':"Besc eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MTcsIm5hbWUiOiJ1c2VyIiwiaWF0IjoxNzE4NDMyODcwLCJleHAiOjE3MTg1MTkyNzB9.jELwlVAwhO0_3Ngpo-K9R3BRsCa-mX3RK2KVYv06OXaR5NyYVraYh2eju8mACSYzgRughI4U8AIQQcN0HdzvignqsW2nv_bib94UKt8H2_C2K_RWSdBr7yzqRrP-8D9-2nQc-Y_U2aKoYfFtsmRRzDq7t61qlyjLiy6cDTuUC-AIlki4egCmvfOrVPJyuHwT_TyC1Cc9v5MuRijr0lzCS6JhE1p95LtPSJlpOX3ugkqW-TbHxan_ofjjutWbi4QgH_ynF9AWYJlWS6XpzM27wEFtX-_vK6rfoAa5biUlPHg6k2821ZTKOyhNRilOc_5H8eVV0R5GC7WDmpC_cw-WPg"})

const handleAvatarSuccess= async(
  response,
  uploadFile
) => { 
  imageUrl.value = URL.createObjectURL(uploadFile.raw)
  let res=await image(uploadFile.raw)
  console.log(res)
}

const beforeAvatarUpload= (rawFile) => {
if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}

const getView=async()=>{
    console.log(await login())
}
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>